package com.example.demo2.student;

import com.example.demo2.Util.DBHelper;
import com.example.demo2.pojo.class1;

import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClasssDaoImp {
    Connection conn = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;

    DBHelper dbHelper = new DBHelper();


    public void update(class1 classs) {
         conn = dbHelper.getConn();
        try {
            preparedStatement = conn.prepareStatement("update webdesign.class set className=? where id=?");
            preparedStatement.setInt(2,classs.getId());
            preparedStatement.setString(1,classs.getClassName());
            int i = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            dbHelper.closeAll(conn,preparedStatement,resultSet);
        }
    }

    public java.util.List<class1> selectAll() {
        conn =dbHelper.getConn();
        ArrayList<class1> class1s = new ArrayList<>();
        try {
            preparedStatement= conn.prepareStatement("select *from webdesign.class ");
            resultSet  = preparedStatement.executeQuery();
            while (resultSet.next()){
                class1 class1 = new class1();
                class1.setId(resultSet.getInt("id"));
                class1.setClassName(resultSet.getString("className"));
                class1s.add(class1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
        }

        return  class1s;
    }
}
